'use strict';
/**
 * @fileoverview 金币活动副本
 * @author agl
 */

var _ = require('lodash');
var moment = require('moment');
var mongoose = require('mongoose');
var dataApi = require('../libs/dataApi');
var configUtil = require('../util/configUtil');
var constantUtil = require('../util/constantUtil');
var mgrUtil = require('../util/mgrUtil');
var eventType = require('../consts/eventType.js');

/**
 * 初始化挑战次数
 * @return {[type]} [description]
 */
var _initFightTimes = function () {
    return constantUtil.value('active_carbon_challenge_times');
};

/**
 * 金币活动副本
 */
var goldCarbonSchema = new mongoose.Schema({
    // 角色id
    role_id: {
        type: Number,
        required: true,
        unique: true,
        index: true
    },

    // 剩余挑战次数
    remain_fight_times: {
        type: Number,
        default: _initFightTimes
    },

    // VIP挑战次数
    vip_fight_times: {
        type: Number,
        default: 0
    },

    // 冷却时间
    freeze_tm: {
        type: Number,
        default: 0
    },

    // 战斗时间
    battle_tm: {
        type: Number,
        default: 0
    },

    // 通关副本
    carbon_passed: {
        type: Number,
        default: 0
    },
});

goldCarbonSchema.pre('save', function (next) {
    this.increment();
    if (!this.isNew && !!this.isModified()) {
        this.pushModify();
    }
    next();
});

/**
 * 获取修改
 * @return {[type]} [description]
 */
goldCarbonSchema.methods.getLastChange = function () {
    var change_info = this.toJSON();
    change_info.open_game_time = configUtil.getOpenGameTime();
    change_info.open_game_date = configUtil.getOpenGameDate();
    return change_info;
};

/**
 * 推送给用户
 * @return {[type]} [description]
 */
goldCarbonSchema.methods.push = function () {
    this.pushModify();
};

/**
 * 推送给用户
 * @return {[type]} [description]
 */
goldCarbonSchema.methods.pushModify = function () {
    var self = this;
    mgrUtil.sendStatusMessage(
        [self.role_id],
        eventType.ON_GOLD_CARBON_CHANGE,
        self.getLastChange(),
        function (err, fails) {
            if (err) {
                console.error('send actCarbon message error: %j, fail ids: %j', err, fails);
                return;
            }
        }
    );
};

if (!goldCarbonSchema.options.toJSON) {
    goldCarbonSchema.options.toJSON = {};
}
/* jshint unused:false */
goldCarbonSchema.options.toJSON.transform = function (doc, ret) {
    delete ret._id;
    delete ret.__v;
};

/**
 * 设置VIP挑战次数
 */
goldCarbonSchema.methods.setFightTimes = function (num) {
    this.vip_fight_times = (+num);
};

/**
 * 增加可挑战次数
 */
goldCarbonSchema.methods.addFightTimes = function (num) {
    this.remain_fight_times += num;
    return true;
};

/**
 * 减少可挑战次数
 */
goldCarbonSchema.methods.decFightTimes = function () {
    if (this.remain_fight_times < 1) {
        return false;
    }
    this.remain_fight_times--;
    return true;
};

/**
 * 重置挑战次数
 */
goldCarbonSchema.methods.resetFightTimes = function () {
    this.remain_fight_times = _initFightTimes();
};

mongoose.model('GoldCarbon', goldCarbonSchema);
